#include "dlg_preserve_local_cmd.h"
#include "ui_dlg_preserve_local_cmd.h"

Dlg_Preserve_Local_CMD::Dlg_Preserve_Local_CMD(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dlg_Preserve_Local_CMD)
{
    ui->setupUi(this);
    this->setWindowFlags(this->windowFlags()| Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
    ui->tableView->setModel(model_7E_LocalCMD);//sql模型与tableView绑定
    ui->tableView->resizeColumnsToContents();//列宽适应内容
    ui->tableView->setSortingEnabled(true);//排序使能
    ui->tableView->hideColumn(0);//隐藏主键列
    ui->lineEditData->setValidator(new QRegExpValidator(QRegExp("[ 0-9A-Fa-f]*"),this));

}

Dlg_Preserve_Local_CMD::~Dlg_Preserve_Local_CMD()
{
    delete ui;
}

void Dlg_Preserve_Local_CMD::on_tableView_clicked(const QModelIndex &index)
{
    int row = index.row();
    QString db_cmd_str = model_7E_LocalCMD->record(row).value("cmd").toString();
    ui->lineEditDID->setText(db_cmd_str);
    bool isOK;
    int data_len = model_7E_LocalCMD->record(row).value("data_len").toString().toInt(&isOK);
    if(!isOK)
        data_len = 0;
    ui->lineEditLen->setText(QString("%1").arg(data_len+1,2,16,(QChar)'0'));
    ui->lineEditData->setText("");
    ui->lineEditData->setPlaceholderText( QString("请输入%1个字节，用空格隔开").arg(data_len)  );
}

void Dlg_Preserve_Local_CMD::on_pushButtonSend_clicked()
{
    uint8 frame[100]={0x7E,  0,0,0,0,  0,0,0,0};
    struct SHS_local_frame *pframe = (struct SHS_local_frame *)frame;
    bool isOK;
    pframe->length = ui->lineEditLen->text().toInt(&isOK,16);
    qDebug("理论pframe->length=0x%X", pframe->length);
    if(!isOK)
    {
        QMessageBox::information(this, tr("错误提示"), tr("长度有误，无法组建本地报文"));
        return;
    }
    pframe->did = ui->lineEditDID->text().toInt(&isOK,16);
    if(!isOK)
    {
        QMessageBox::information(this, tr("错误提示"), tr("did有误，无法组建本地报文"));
        return;
    }

    QString data_str = ui->lineEditData->text();
    int data_input_len = hex_str_to_byte_arr(data_str, pframe->data, sizeof(frame)-9,true);
    qDebug("data域(不含did)输入的字节数=0x%X", data_input_len);
    int row = ui->tableView->currentIndex().row();
    int data_len_theory = model_7E_LocalCMD->record(row).value("data_len").toString().toInt(&isOK);
    if(!isOK)
        data_len_theory = 0;

    if(data_input_len != data_len_theory)
    {
        QMessageBox::information(this, tr("错误提示"), tr("输入的data域字节数有误，无法组建本地报文"));
        return;
    }

    pframe->data[pframe->length-1] = checksum(frame, SHS_FRAME_HEAD+pframe->length);

    qDebug("组建好的pframe->length=0x%X", pframe->length);
    qDebug("组建好的pframe->sum=0x%X", pframe->data[pframe->length-1]);

    int frame_total_len = SHS_FRAME_HEAD+pframe->length +1;


    emit send_local_frame((const char *)pframe,frame_total_len);

}

void Dlg_Preserve_Local_CMD::on_pushButtonInsetRow_clicked()
{
    int rowNum = model_7E_LocalCMD->rowCount(); //获得表的行数
    model_7E_LocalCMD->insertRow(rowNum); //添加一行
    model_7E_LocalCMD->setData(model_7E_LocalCMD->index(rowNum,1),"");//至少要执行一次写入操作，否则存在空行，会提交失败
    if(model_7E_LocalCMD->submitAll())
    {
        qDebug()<<"添加行成功";
    }
    else
    {
        qDebug()<<"添加行失败:"<<model_7E_LocalCMD->lastError().text();
    }
}

void Dlg_Preserve_Local_CMD::on_pushButtonDelRow_clicked()
{
    qDebug()<<"要删除的行："<<ui->tableView->currentIndex().row();
    qDebug()<<"删除操作的结果是："<<model_7E_LocalCMD->removeRow(ui->tableView->currentIndex().row());
    qDebug()<<"提交删除的结果是："<<model_7E_LocalCMD->submitAll();//提交修改
    model_7E_LocalCMD->setFilter("");//相当于刷新tableView的显示
}

void Dlg_Preserve_Local_CMD::on_pushButtonSave_clicked()
{
    if(model_7E_LocalCMD->submitAll())
    {
        qDebug()<<"保存成功";
    }
    else
    {
        qDebug()<<"保存失败:";
    }


    model_7E_LocalCMD->setFilter("");//相当于刷新tableView的显示
}
